package view;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JCheckBox;

import javax.swing.JFrame;

/**
 * This code was edited or generated using CloudGarden's Jigloo
 * SWT/Swing GUI Builder, which is free for non-commercial
 * use. If Jigloo is being used commercially (ie, by a corporation,
 * company or business for any purpose whatever) then you
 * should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details.
 * Use of Jigloo implies acceptance of these licensing terms.
 * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
 * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
 * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
@SuppressWarnings("serial")
public class Rules extends JFrame implements KeyListener {
	
	private AbstractAction okButtonAction;
	private AbstractAction cancelAction;
	private AbstractAction enterGoalEasilyAction;
	private AbstractAction allowPushAction;
	private JButton okButton;
	private JButton cancelButton;
	private JCheckBox allowPush;
	private JCheckBox allowEnterGoalEasy;
	private boolean allowPushBool = false;
	private boolean enterGoalEasilyBool = false;
	//private transient IRules r = new model.Rules();
	
	public Rules() {
		initGUI();
		setVisible(true);
		setLocationRelativeTo(null);
		setFocusable(true);
		addKeyListener(this);
	}

	private void initGUI() {
		try {
			{
				getContentPane().setLayout(null);
				this.setPreferredSize(new java.awt.Dimension(400, 400));
				this.setTitle("Set Game Rules");
				this.setResizable(false);
				{
					okButton = new JButton();
					getContentPane().add(okButton);
					getContentPane().add(getAllowPush());
					getContentPane().add(getAllowEnterGoalEasy());
					getContentPane().add(getCancelButton());
					okButton.setBounds(313, 318, 55, 35);
					okButton.setAutoscrolls(true);
					okButton.setAction(getOkButtonAction());
					okButton.setText("OK");
				}
				this.setSize(400, 400);
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	private AbstractAction getOkButtonAction() {
		if(okButtonAction == null) {
			okButtonAction = new AbstractAction("OK", null) {
				public void actionPerformed(ActionEvent evt) {
//					if(allowPushBool)
//						r.setPushAllowedOn();
//					else
//						r.setPushAllowedOff();
//					if(enterGoalEasilyBool)
//						r.setEnterGoalEasyOn();
//					else
//						r.setEnterGoalEasyOff();
//					EventBus.publish(Events.CHANGE_RULES.toString(), r);
//					setVisible(false);
				}
			};
		}
		return okButtonAction;
	}

	private JCheckBox getAllowPush() {
		if(allowPush == null) {
			allowPush = new JCheckBox();
			allowPush.setText("Allow Push");
			allowPush.setBounds(12, 25, 152, 20);
			allowPush.setToolTipText("Allows you to push a foe's player piece back to his/her home.");
			allowPush.setAction(getAllowPushAction());
			allowPush.setMnemonic('a');
		}
		return allowPush;
	}

	private JCheckBox getAllowEnterGoalEasy() {
		if(allowEnterGoalEasy == null) {
			allowEnterGoalEasy = new JCheckBox();
			allowEnterGoalEasy.setText("Enter Goal Easily");
			allowEnterGoalEasy.setBounds(12, 64, 152, 20);
			allowEnterGoalEasy.setToolTipText("With this setting checked, you can enter the goal eventhough you might get a die value taking you to far.");
			allowEnterGoalEasy.setAction(getEnterGoalEasilyAction());
			allowEnterGoalEasy.setMnemonic('e');
		}
		return allowEnterGoalEasy;
	}
	
	private JButton getCancelButton() {
		if(cancelButton == null) {
			cancelButton = new JButton();
			cancelButton.setText("Cancel");
			cancelButton.setBounds(224, 318, 78, 35);
			cancelButton.setAction(getCancelAction());
		}
		return cancelButton;
	}
	
	private AbstractAction getAllowPushAction() {
		if(allowPushAction == null) {
			allowPushAction = new AbstractAction("Allow Push", null) {
				public void actionPerformed(ActionEvent evt) {
					if(allowPush.isSelected())
						allowPushBool = false;
					else
						allowPushBool = true;
				}
			};
		}
		return allowPushAction;
	}
	
	private AbstractAction getEnterGoalEasilyAction() {
		if(enterGoalEasilyAction == null) {
			enterGoalEasilyAction = new AbstractAction("Enter Goal Easily", null) {
				public void actionPerformed(ActionEvent evt) {
					if(allowEnterGoalEasy.isSelected())
						enterGoalEasilyBool = false;
					else
						enterGoalEasilyBool = true;
				}
			};
		}
		return enterGoalEasilyAction;
	}
	
	private AbstractAction getCancelAction() {
		if(cancelAction == null) {
			cancelAction = new AbstractAction("Cancel", null) {
				public void actionPerformed(ActionEvent evt) {
					setVisible(false);
				}
			};
		}
		return cancelAction;
	}

	@Override
	public void keyPressed(KeyEvent e) {
		Character c = e.getKeyChar();
		if(c.equals('\n')) {
			okButton.doClick();
		}
		System.out.println(e.getKeyCode());
		if(e.getKeyCode() == 27)
			cancelButton.doClick();
		
	}

	@Override
	public void keyReleased(KeyEvent e) {}

	@Override
	public void keyTyped(KeyEvent e) {}
}